package com.sbtr.config;

import java.util.concurrent.Executor;
import java.util.concurrent.ThreadPoolExecutor;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

/**
 * Spring Async异步调用配置
 * 
 * @author oujh5
 *
 */
@Configuration
public class AsyncConfig {

	@Value("${async.pool.core-size:10}")
	private int coreSize;
	@Value("${async.pool.max-size:20}")
	private int maxSize;
	@Value("${async.pool.queue-capacity:200}")
	private int queueCapacity;
	@Value("${async.pool.keep-alive-seconds:60}")
	private int keepAliveSeconds;

	@Value("${async.pool.core-size:10}")
	private int coreSizeTxt;
	@Value("${async.pool.max-size:40}")
	private int maxSizeTxt;

	@Bean("asyncExecutor")
	public Executor asyncExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(coreSize);
		executor.setMaxPoolSize(maxSize);
		executor.setQueueCapacity(queueCapacity);
		executor.setKeepAliveSeconds(keepAliveSeconds);
		executor.setThreadNamePrefix("async-");
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
		return executor;
	}

	@Bean("asyncTxtExecutor")
	public Executor asyncTxtExecutor() {
		ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
		executor.setCorePoolSize(coreSizeTxt);
		executor.setMaxPoolSize(maxSizeTxt);
		executor.setQueueCapacity(queueCapacity);
		executor.setKeepAliveSeconds(keepAliveSeconds);
		executor.setThreadNamePrefix("txt-");
		executor.setRejectedExecutionHandler(new ThreadPoolExecutor.AbortPolicy());
		return executor;
	}
}
